/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.service.content;

import han.bifang.gwt.client.common.EntityConstants;
import han.bifang.gwt.client.common.entity.MethodQos;
import han.bifang.gwt.client.service.ServiceMessages;

import java.util.ArrayList;
import java.util.HashMap;

import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;

/**
 * 
 * @Author Kent
 */
public class ShowMethodQosPanel extends Composite {
	private ServiceMessages cons;

	public ShowMethodQosPanel(ServiceMessages c, ArrayList qos) {
		this.cons = c;

		HashMap<String, String> tmp = new HashMap<String, String>();
		for (int i = 0, j = qos.size(); i < j; i++) {
			MethodQos q = (MethodQos) qos.get(i);
			tmp.put(q.getName(), q.getValue());

		}

		FlexTable table = new FlexTable();
		table.setCellPadding(1);
		table.setCellSpacing(1);
		table.setBorderWidth(1);

		ColumnFormatter cf = table.getColumnFormatter();
		cf.setWidth(0, "15%");
		cf.setWidth(1, "65%");

		CellFormatter ff = table.getCellFormatter();

		ff.setStyleName(0, 0, "tablecol");
		table.setHTML(0, 0, cons.commType());

		String commType = tmp.get(EntityConstants.QOS_COMM_TYPE);
		if (commType.equals(EntityConstants.ASYNC)) {
			table.setHTML(0, 1, cons.async());

		} else if (commType.equals(EntityConstants.SYNC)) {
			table.setHTML(0, 1, cons.sync());

		}
		if (commType.equals(EntityConstants.ASYNC_WITH_REPLY)) {
			table.setHTML(0, 1, cons.asyncWithReply());

		}

		ff.setStyleName(1, 0, "tablecol");
		table.setHTML(1, 0, cons.timeout());

		table.setHTML(1, 1, tmp.get(EntityConstants.QOS_TIMEOUT));
		int row = table.getRowCount();
		table.insertRow(row);
		ff.setStyleName(row, 0, "tablecol");
		table.setHTML(row, 0, cons.warn());
		CheckBox warn = new CheckBox();
		warn.setEnabled(false);
		table.setWidget(row, 1, warn);
		if (tmp.get(EntityConstants.QOS_WARN_ERROR) != null
				|| tmp.get(EntityConstants.QOS_WARN_RESPONSE_TIME) != null) {
			warn.setChecked(true);
			if (tmp.get(EntityConstants.QOS_WARN_ERROR) != null) {
				VerticalPanel h1 = new VerticalPanel();

				CheckBox errorEnable = new CheckBox(cons.enable());
				errorEnable.setChecked(true);
				errorEnable.setEnabled(false);
				h1.add(errorEnable);

				h1.add(new HTML("&nbsp;"));

				h1.add(new HTML(QosUtil.errorXmlToString(cons, tmp
						.get(EntityConstants.QOS_WARN_ERROR))));

				row = table.getRowCount();

				table.insertRow(row);

				ff.setStyleName(row, 0, "tablecol");
				table.setHTML(row, 0, cons.warnError());
				table.setWidget(row, 1, h1);

			}
			if (tmp.get(EntityConstants.QOS_WARN_RESPONSE_TIME) != null) {
				VerticalPanel h1 = new VerticalPanel();

				CheckBox resEnable = new CheckBox(cons.enable());
				resEnable.setChecked(true);
				resEnable.setEnabled(false);
				h1.add(resEnable);

				h1.add(new HTML("&nbsp;"));

				h1.add(new HTML(QosUtil.resQosXmlToString(cons, tmp
						.get(EntityConstants.QOS_WARN_RESPONSE_TIME))));

				row = table.getRowCount();

				table.insertRow(row);

				ff.setStyleName(row, 0, "tablecol");
				table.setHTML(row, 0, cons.warnError());
				table.setWidget(row, 1, h1);

			}

		}

		initWidget(table);

	}

	public String getDisplay(String value) {
		if (value.equals(EntityConstants.QOS_YES)) {
			return cons.yes();
		} else if (value.equals(EntityConstants.QOS_NO)) {
			return cons.no();
		} else if (value.equals(EntityConstants.QOS_DEFAULT)) {
			return cons.default1();
		}
		return null;
	}
}
